package cn.yyx.aclservice.controller;
import cn.yyx.aclservice.entity.User;
import cn.yyx.common.R;
import cn.yyx.aclservice.service.RoleService;
import cn.yyx.aclservice.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
 * <p>
 * 用户表前端控制器
 * </p>
 * @author testjava
 * @since 2020-01-12
 */
@RestController
@RequestMapping("/admin/acl/users")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @ApiOperation(value = "获取管理用户分页列表")
    @GetMapping("{page}/{limit}")
    /**
     * userQueryVo 查询条件
     */
    public R index(
            @PathVariable Long page,
            @PathVariable Long limit,
                User userQueryVo) {
        Page<User> pageParam = new Page<>(page, limit);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(userQueryVo.getUsername())) {
            wrapper.like("username",userQueryVo.getUsername());
        }
        IPage<User> pageModel = userService.page(pageParam, wrapper);
        return R.ok().data("items", pageModel.getRecords()).data("total", pageModel.getTotal());
    }
    /**
     * 根据用户的编号查询用户信息
     * 修改数据的时候要回显
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public R getUser(@PathVariable String  id){
        return R.ok().data("item",userService.getById(id));
    }
    /**
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "新增管理用户")
    @PostMapping("save")
    public R save(@RequestBody User user) {
        //对原始密码加密之后再存储到数据库
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        return userService.save(user)? R.ok():R.error();
    }
    /**
     * 编辑用户信息
     * @param user
     * @return
     */
    @ApiOperation(value = "修改管理用户")
    @PutMapping("update")
    public R updateById(@RequestBody User user) {
        return userService.updateById(user)?R.ok():R.error();
    }
    /**
     * 根据编号删除数据
     * @param id
     * @return
     */
    @ApiOperation(value = "删除管理用户")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        return  userService.removeById(id)?R.ok():R.error();
    }
    @ApiOperation(value = "根据id列表删除管理用户")
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<String> idList) {
       return  userService.removeByIds(idList)? R.ok():R.error();
    }
    /**
     * 取得用户角色
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public R toAssign(@PathVariable String userId) {
        Map<String, Object> roleMap = roleService.findRoleByUserId(userId);
        return R.ok().data(roleMap);
    }
    /**
     * 分配角色
     * @param userId
     * @param roleId
     * @return
     */
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public R doAssign(@RequestParam String userId,@RequestParam String[] roleId) {
        //先删除该用户原来的所有权限--->之后重新添加
        return  roleService.saveUserRoleRealtionShip(userId,roleId)?R.ok():R.error();
    }
}

